package com.wjm.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wjm.entity.Classroom;
import com.wjm.entity.Desk;
import com.wjm.service.impl.ClassroomServiceImpl;
import com.wjm.service.impl.DeskServiceImpl;
import com.wjm.util.CacheUtil;
import com.wjm.util.StringUtil;

@Controller
@RequestMapping("/web/classroom")
public class ClassroomWebController {

	@Resource(name = "classroomService")
	private ClassroomServiceImpl classroomService;

	@Resource(name = "deskService")
	private DeskServiceImpl deskService;

	private final String listKey = "list";
	private final String pagesKey = "pages";
	private final String pageKey = "page";
	private final String statusKey = "status";
	private final String buKey = "buId";

	@RequestMapping("/query")
	public String query(int buId, int page, int status, Model model) {
		String result = "";
		try {
			int number = Integer.parseInt(CacheUtil.getProperties().getProperty(StringUtil.CLASSROOM_NUM));
			int total = classroomService.queryTotal(buId, status);
			int pages = total % number == 0 ? (total / number) : (total / number + 1);
			List<Classroom> list = null;
			if (page > 0 && page <= pages) {
				Classroom classroom = new Classroom();
				classroom.setBelongBu(buId);
				classroom.setCrStatus(status);
				list = classroomService.queryByPage(page, number, classroom);
			} else {
				list = new ArrayList<Classroom>();
			}

			model.addAttribute(listKey, list);
			model.addAttribute(pageKey, page);
			model.addAttribute(pagesKey, pages);
			model.addAttribute(statusKey, status);
			model.addAttribute(buKey, buId);
			result = "/web/building/classroom-show";
		} catch (Exception e) {
			e.printStackTrace();
			result = "error";
		}
		return result;
	}

	@RequestMapping("/add")
	@ResponseBody
	public String add(Classroom cr) {
		String result = "";
		try {
			if (classroomService.save(cr)) {
				// 将添加的教室放入不可用队列（刚刚添加的教室还没有添加座位）
				List<Classroom> list = CacheUtil.getUnuseroom().get(cr.getBelongBu());
				if (list == null) {
					list = new ArrayList<>();
				}
				list.add(cr);
				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

	@RequestMapping("/del")
	@ResponseBody
	public String delete(int crId, int buId) {
		String result = "";
		try {
			if (crId > 0 && classroomService.delete(crId)) {

				List<Classroom> list = CacheUtil.getUnuseroom().get(buId);
				boolean flag = true;
				if (list != null)
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).getCrId() == crId) {
							flag = false;
							list.remove(i);
						}
					}
				if (flag == true) {
					list = CacheUtil.getUseroom().get(buId);
					if (list != null)
						for (int i = 0; i < list.size(); i++) {
							if (list.get(i).getCrId() == crId) {
								list.remove(i);
							}
						}
				}

				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}

	@RequestMapping("/update")
	@ResponseBody
	public String update(int crId, int status) {
		String result = "";
		try {
			Classroom cr = new Classroom();
			cr.setCrId(crId);
			cr.setCrStatus(status);
			if ((status == 1 || status == 2) && classroomService.update(cr)) {

				// 修改缓存中的状态
				List<Desk> desks = CacheUtil.getDesks().get(crId);
				if (desks != null)
					for (Desk desk : desks) {
						desk.setDeskStatus(status);
					}

				result = "{\"result\":\"success\"}";
			} else {
				result = "{\"result\":\"fail\"}";
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "{\"result\":\"error\"}";
		}
		return result;
	}
}
